package com.bms.admin.util;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.reflections.Reflections;
import org.springframework.web.bind.annotation.RestController;

import com.bms.auth.annotation.RequiresPermissions;
import com.feilong.core.Validator;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


public class AnnoManageUtil {
	/**
     * 获取指定文件下面的RequestMapping方法保存在mapp中
     *
     * @param packageName
     * @return
     */
    public static Map<String,Map<String, String>> getRequestMappingMethod(String packageName) {
    	Reflections reflections = new Reflections(packageName);
		Set<Class<?>> classesList = reflections.getTypesAnnotatedWith(RestController.class);

		Map<String, Map<String, String>> allMaps = new HashMap<>();		
		Long parentId = 0L;
		for (Class classes : classesList) {
			if(classes.isAnnotationPresent(Api.class)){
				Map<String, String> mapp = new HashMap<String, String>();		
				Api anns = (Api)classes.getAnnotation(Api.class);
				System.out.println();
				
				// 得到该类下面的所有方法
				Method[] methods = classes.getDeclaredMethods();
				
				for (Method method : methods) {
					RequiresPermissions requiresPermissions = method.getAnnotation(RequiresPermissions.class);
					ApiOperation apiOperation = method.getAnnotation(ApiOperation.class);
					if (Validator.isNotNullOrEmpty(requiresPermissions) && Validator.isNotNullOrEmpty(apiOperation)) {
						String[] perValues = requiresPermissions.value();
						String operationValues = apiOperation.value();
						for (String perValue : perValues) {
							mapp.put(perValue, operationValues);
						}
					}
				}
				allMaps.put(anns.tags()[0], mapp);
			}
		}
		return allMaps;
    }
}
